<?php
class RegisterController extends Controller {
	public function index($params) {
		$smaryt = $this->getSmarty ();
		$this->smarty->assign ( 'pagesets', $this->headerDatas () );
		// 如果不是post方式的提交，直接转向
// 		if (! CommonBase::isPost ()) {
// 			$this->smarty->display ( "user_center/register.tpl" );
// 			return;
// 		}
		
		$invaitecode = $params [0];
		if (! empty ( $invaitecode )) {
			$this->smarty->assign ( "invaiteid", $invaitecode );
		}
		
		$email = $_POST ["email"];
		$invaiteid = $_POST ["invaiteid"];
		if (empty ( $email )) {
			$this->smarty->assign ( "errortip", $this->userExsist ( '必须填写Email' ) );
			
			$this->smarty->display ( "user_center/register.tpl" );
		}
		require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
		$userService = new UsersService ( $this->getDB () );
		
		$user = $userService->getEmailByName ( $email );
		
		if (! empty ( $user->email )) {
			$this->smarty->assign ( "errortip", $this->userExsist ( '此用户已经存在，不能重复注册' ) );
			$this->smarty->assign ( "email", $email );
			$this->smarty->assign ( "password", $_POST ["password"] );
			$this->smarty->display ( "user_center/register.tpl" );
			$this->smarty->assign ( "invaiteid", $invaiteid );
			return;
		}
		
		$emailArr = explode ( "@", $email );
		$uid = uniqid ();
		$user = array (
				'userid' => $uid,
				'login_type' => 0,
				'username' => $emailArr [0],
				'password' => md5 ( $_POST ["password"] ),
				'email' => $_POST ["email"],
				'user_invite_code' => $uid,
				'invite_userid' => $invaiteid,
				'points' =>200
				);
		$userService->addUser ( $user );
		
		if(!empty($invaiteid)){
			//添加邀请人记录信息表
			$user = $userService->getUserInfoByID($invaiteid);
			$userInviteRecord = array (
					'id' => uniqid (),
					'userid' => $user->userid,
					'username' => $user->username,
					'invite_user_id' => $uid,
					'invite_user_mail' => $_POST ["email"],
					'state' => 1,
					'create_time' =>date("Y-m-d h:m:s")
			);
			$userService->addUserInviteRecord($userInviteRecord);
		}
		
		//新注册用户添加200积分的基本积分
		
		
		
		$_SESSION ['user'] = $user;
		
		require_once COMMON . DS . 'SendMailUtil.class.php';
		SendMailUtil::sendmail ( "Welcome to babyspeedy", $email . ",register success", $email );
		$this->smarty->assign ( "errortip", $this->userSuccess ( 'success' ) );
		$this->smarty->assign ( "success", true );
		$this->smarty->display ( "user_center/register.tpl" );
	}
	public function handreg() {
		$this->getSmarty ();
	}
	public function userExsist($msg) {
		return "<div class='alert alert-error'>$msg</div>";
	}
	public function userSuccess($msg) {
		return "<div class='alert alert-success'>$msg</div>";
	}
	public function headerDatas() {
		require_once COMMON . DS . 'Constant.class.php';
		$js = array (
				WEBSITE_URL . "public/assets/js/jquery.validationEngine-zh.js",
				WEBSITE_URL . "public/assets/js/jquery.validationEngine.js" 
		);
		$css = array (
				WEBSITE_URL . "public/assets/css/validationEngine.jquery.css" 
		);
		$pagesets = array (
				"seo" => Constant::$seoarray,
				"js" => $js,
				"css" => $css,
				"nav" => Constant::getNav () 
		);
		return $pagesets;
	}
}